package com.fjwt.gz.province.changchun.encrypt;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * AES  @RequestBody参数解密
 *
 * @author Lee
 * @date 2024-11-22
 */
@Service
public class DecryptService {

    private final ObjectMapper objectMapper = new ObjectMapper();

    public <T> T decryptAndDeserialize(String encryptedJson, Class<T> clazz) {
        try {
            String decryptedJson = AesUtil.decrypt(encryptedJson);
            return objectMapper.readValue(decryptedJson, clazz);
        } catch (Exception e) {
            throw new RuntimeException("解密或反序列化失败", e);
        }
    }

    public <T> List<T> decryptJsonList(String encryptedJson, Class<T> clazz) {
        try {
            String decryptedJson = AesUtil.decrypt(encryptedJson);
            return objectMapper.readValue(decryptedJson, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz));
        } catch (Exception e) {
            throw new RuntimeException("解密或反序列化失败", e);
        }
    }

}
